package Leave

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/models"
	"project/pkg/validate"
	"strconv"
	"time"
)

func (h LHandler) ToConfirmStudent(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateStatusReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Leave:ToConfirmStudent with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Leave:ToConfirmStudent with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//查找
	exist, info, err := h.M.LeaveCheckExist(strconv.Itoa(req.LeaveId))
	if err != nil {
		zap.L().Error("Leave:ToConfirmStudent h.M.LeaveCheckExist err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		//更新 -假条
		info.TeacherConfirm = true
		info.TeacherConfirmTime = time.Now().Format("2006-01-02 15:04:05")
		info.Action = "老师确认-学生请假中"
		err := h.M.LeaveUpdateInfoBuID(info)
		if err != nil {
			zap.L().Error("Leave:ToConfirmStudent h.M.LeaveUpdateInfoBuID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		//更新学生
		sexist, studentInfo, err := h.M.StudentCheckExistByOpenID(info.RequestID)
		if err != nil {
			zap.L().Error("Leave:ToConfirmStudent h.M.StudentCheckExistByOpenID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		if sexist {
			studentInfo.LeaveStatus = true
			//更新学生
			err := h.M.StudentUpdateInfoBuID(studentInfo)
			if err != nil {
				zap.L().Error("Leave:ToConfirmStudent h.M.StudentUpdateInfoBuID err", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
		}
		resp.SendSuccess(c)
	}
}
func (h LHandler) ToReturnConfirmStudent(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateStatusReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Leave:ToConfirmStudent with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Leave:ToConfirmStudent with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//查找
	exist, info, err := h.M.LeaveCheckExist(strconv.Itoa(req.LeaveId))
	if err != nil {
		zap.L().Error("Leave:ToConfirmStudent h.M.LeaveCheckExist err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		//更新
		info.ReturnConfirm = true
		info.ReturnConfirmID = strconv.Itoa(req.ReturnId)
		info.ReturnConfirmTime = time.Now().Format("2006-01-02 15:04:05")
		info.ReturnConfirmName = req.ReturnName
		info.Action = "学生结束请假流程"
		err := h.M.LeaveUpdateInfoBuID(info)
		if err != nil {
			zap.L().Error("Leave:ToConfirmStudent h.M.LeaveUpdateInfoBuID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		//更新学生
		sexist, studentInfo, err := h.M.StudentCheckExistByOpenID(info.RequestID)
		if err != nil {
			zap.L().Error("Leave:ToConfirmStudent h.M.StudentCheckExistByOpenID err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		if sexist {
			studentInfo.LeaveStatus = false
			//更新学生
			err := h.M.StudentUpdateInfoBuID(studentInfo)
			if err != nil {
				zap.L().Error("Leave:ToConfirmStudent h.M.StudentUpdateInfoBuID err", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
		}
		resp.SendSuccess(c)
	}
}
